/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import datagram.Datagram;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.HashMap;

/**
 *
 * @author rmb
 */
public class Channel extends Thread {
    
    
    private DatagramSocket socket;
    private HashMap<String, Connection> connList;
    private static final int sendPort = 3030;
    private static final int receivePort = 3030;
    
    private boolean stop;
    
    public Channel(boolean receive) {
        
        connList = new HashMap<String, Connection>();
        stop = false;
        
        try {
            if(receive == true)
                socket = new DatagramSocket(receivePort);
            else
                socket = new DatagramSocket(sendPort);
        }
        catch(SocketException e) {}        
    }
    
    @Override
    public synchronized void run() {
        
        while(!stop) {
            
            byte[] buff = new byte[512];
            DatagramPacket dp = new DatagramPacket(buff, buff.length);
            try {
                socket.receive(dp);
            }
            catch(IOException e) {}
            
            Datagram pack = Datagram.byteToDatagram(dp.getData());
            
            switch (pack.getType()) {
                
                case CONNECT :
                    try {
                        DatagramSocket ds = new DatagramSocket(sendPort);                        
                    }
                    catch(SocketException e) {}
                    
                    Connection con = new Connection(socket, dp.getPort(), dp.getAddress());
                    connList.put(dp.getAddress().getHostAddress(), con);
                    con.getSend().sendCONNECT();
                    
                    con.start();
                    break;
                    
                default:
                    System.out.println("Wrong Packet");
               
                    
            }            
        }
        
    }
    
    
    public synchronized void deleteCon(String ip) {
        Connection c = connList.get(ip);
        c.endConnection();
        
        connList.remove(ip);                
    }
    
    public Object[] getClientes() {
        return connList.values().toArray();
    }
    
    public Connection getConnection(String ip) {
        return (Connection) connList.get(ip);
    }
    
    
    public void sendNotAccepted(InetAddress addr){
        Datagram d = new Datagram(Datagram.UDPDatagram.DISCONNECT, -1, null);
        byte[] send = Datagram.toByteArray(d);
        DatagramPacket dp = new DatagramPacket(send, send.length, addr, sendPort);
        
        try {
            socket.send(dp);
        }
        catch(IOException e) {}
    }
    
    public void setStop() {
        stop();
        socket.close();
        System.out.println("STOPPED");
        
    }
    
}
